Hi dee Ho again.
I started learning how to toy with sockets yesterday... Well, I am utterly confused with the structures && functions dealing with ports & hostnames, with client & server sockets, with this & that
Basically what I would like to do is connect to certain server(s) & be able to send & receive data. I've done that with php sockets, so I assume I should be able to do it with c++ too
I prepared following class for that:
connect.h:
Code:
#ifndef _CONNECT
#define _CONNECT
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
class Connect {
private:
int sock;
struct sockaddr_in serv_addr;
public:
Connect(){;}
~Connect()
{
close(this->sock);
}
int con(string host, int port); //open socket
int send_command(string command);
int send_payload(string command);
char *receive();
};
#endif
and connect.cpp:
Code:
#include "connect.h"
#include <iostream>
#include <string.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ctype.h>
#include <stdlib.h>
#include <netdb.h>
using namespace std;
int Connect::con(string host, int port)
{
struct hostent *h;
char *ip;
//int success;
if((h=gethostbyname(host.c_str()))==NULL)
{
herror("gethostbyname");
exit(1);
}
ip=inet_ntoa(*((struct in_addr *)h->h_addr));
if((this->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) //Lets make the filedescriptor
{
cout << "Smelly Socks!" << endl;
exit(-666);
}
/*Lets connect to server*/
serv_addr.sin_family = AF_INET;
if( inet_aton(ip,&serv_addr.sin_addr) !=0 )
{
cout << "invalid ip format: " << ip << endl;
exit(1);
}
serv_addr.sin_port = htons(port);
if(connect(sock,(struct sockaddr *) &serv_addr, sizeof(serv_addr))<0)
{
cout << "Connecting to " << ip << " failed" << endl;
exit(-666);
}
return 1;
}
int Connect::send_command(string command)
{
int len;
len=strlen(command.c_str())+2;
command=command+"\r\n";
write(sock,command.c_str(),len);
cout << "SENT : " << command << endl;
return 1;
}
int Connect::send_payload(string command)
{
int len;
len=strlen(command.c_str());
cout << "SENT : " << command << endl;
write(sock,command.c_str(),len);
return 1;
}
char *Connect::receive()
{
char *buf;
int nread;
nread=read(this->sock,buf,1024);
cout << "GOT : " << buf << endl;
return buf;
}
Now when I try to use it to connect to a certain server & send command inet_aton fails (it returns something that's not 0, and according to man pages it should not.)
(this
Code:
if( inet_aton(ip,&serv_addr.sin_addr) !=0 )
{
cout << "invalid ip format: " << ip << endl;
exit(1);
}
Any ideas what's wrong? And can use this kind of class to do the connection? As I said, I am a noob with sockets, and the structures & handling of ip's / hostnames are mostly just taken from examples...
If someone feels like explaining them (structures & functions to manipulate hostnames/ips) well, feel free, (that would really help me understanding them better, cause the tutos I found just stated there is such structures, but the purpose is not terribly clear :/) but I would also be gratefull if someone just could tell me why inet_aton does not work.
Oh, BTW. I am using linux & g++ compiler.